package com.hotel.service;

import com.hotel.mapper.HotelMapper;
import com.hotel.mapper.OrderMapper;
import com.hotel.mapper.RoomMapper;
import com.hotel.mapper.UserMapper;
import com.hotel.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RoomMapper roomMapper;

    // 注册
    public boolean registerUser(User user) {
        User userByName = userMapper.findUserByName(user.getName());
        User userByUserName = userMapper.findUserByUserName(user.getUsername());
        if (userByName != null || userByUserName != null) {
            return false;
        } else {
            userMapper.addUser(user);
            return true;
        }
    }

    // 用户登录
    public void loginUser(String name) {
        userMapper.loginUser(name);
    }

    // 用户注销
    public void exitUser(String name) {
        userMapper.exitUser(name);
    }

    // 检查用户是否登录
    public boolean isLogin(String name) {
        return userMapper.isLogin(name) == 1;
    }

    // 检验用户账户密码
    public boolean checkUser(User user) {
        List<User> userList = userMapper.findAllUser();
        List<User> collect = userList.stream().filter(user1 -> user.getUsername().equals(user1.getUsername())
                && user.getPassword().equals(user1.getPassword())).collect(Collectors.toList());
        return !collect.isEmpty();
    }

    // 查看用户是否存在
    public boolean findUserByName(String name) {
        User userByName = userMapper.findUserByName(name);
        return userByName != null;
    }

    // 修改个人信息
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    //查询酒店详细信息
    public List<Hotel> findAllHotel() {
        List<Hotel> hotelList = hotelMapper.findAllHotel();
        for(Hotel hotel : hotelList) {
            List<Room> roomList = roomMapper.findRoomByHotel(hotel.getName());
            hotel.setRoomList(roomList);
        }
        return hotelList;
    }

    // 根据名称查询酒店
    public Hotel findHotelByName(String name) {
        return hotelMapper.findHotelByName(name);
    }

    // 预订酒店
    public boolean orderHotel(Order order) {
        Room room = new Room();
        room.setHotelName(order.getHotelName());
        room.setType(order.getType());
        Room roomByHotelAndType = roomMapper.findRoomByHotelAndType(room);
        int price = roomByHotelAndType.getPrice();
        int num = roomByHotelAndType.getNum();
        if (num >= order.getRoomCount()) {
            order.setPrice(price);
            orderMapper.addOrder(order);
            roomByHotelAndType.setNum(num - order.getRoomCount());
            roomMapper.updateRoom(roomByHotelAndType);
            return true;
        } else {
            return false;
        }
    }

    // 查看订单
    public List<Order> findOrderByName(String name) {
        return orderMapper.findOrderByName(name);
    }

    // 查看所有订单
    public List<Order> findAllOrder() {
        return orderMapper.findAllOrder();
    }

}
